//
//  ACAudicenceMicViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/1.
//

#import "ACAudicenceMicViewController.h"
#import "ACAnchorMemberListCell.h"
#import "ACLiveMemberListCell.h"
#import "ACLiveModel.h"

@interface ACAudicenceMicViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;

@end

@implementation ACAudicenceMicViewController
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 68;
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACAnchorMemberListCell" bundle:curB] forCellReuseIdentifier:@"ACAnchorMemberListCell"];
        [_tableview registerNib:[UINib nibWithNibName:@"ACLiveMemberListCell" bundle:curB] forCellReuseIdentifier:@"ACLiveMemberListCell"];
        _tableview.tableFooterView = [UIView new];
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = _isOnMic ? @"麦上观众" : @"请求发言观众";
    label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    label.textColor = UIColorFromRGB(0x30322C);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_offset(16);
    }];
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageWithName:@"live_live_close_gray" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(46, 46));
        make.centerY.equalTo(label.mas_centerY);
    }];
    @weakify(self)
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-ACSafeAreaBottomHeight);
        make.top.mas_equalTo(label.mas_bottom).offset(28);
    }];
}
-(void)setIsOnMic:(BOOL)isOnMic{
    _isOnMic = isOnMic;
}
-(void)setRequestJoinAnchorArray:(NSMutableArray<TRTCLiveUserInfo *> *)requestJoinAnchorArray{
    _requestJoinAnchorArray = requestJoinAnchorArray;
    [self.tableview reloadData];
}

-(void)setJoinAnchorArray:(NSMutableArray<TRTCLiveUserInfo *> *)joinAnchorArray{
    _joinAnchorArray = joinAnchorArray;
    [self.tableview reloadData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.isOnMic ? self.joinAnchorArray.count : self.requestJoinAnchorArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isOnMic) {
        ACLiveMemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACLiveMemberListCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[ACLiveMemberListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACLiveMemberListCell"];
        }
        [cell.cancelForbiden setTitle:@"下麦" forState:UIControlStateNormal];
        [cell.cancelForbiden setHidden:NO];
        TRTCLiveUserInfo *userInfo = self.joinAnchorArray[indexPath.row];
        cell.userInfo = userInfo;
        @weakify(self)
        cell.clickActionHandler = ^(NSInteger value) {
            @strongify(self)
            [self.liveRoom kickoutJoinAnchor:userInfo.userId callback:^(int code, NSString * _Nullable message) {
                
            }];
            [SVProgressHUD showInfoWithStatus:@"我踢出了正在连麦的用户"];
            [self.joinAnchorArray removeObject:userInfo];
            [self.tableview reloadData];
            if (self.removeUserHandler) {
                self.removeUserHandler(userInfo,YES);
            }
        };
        return  cell;
    }else{
        ACAnchorMemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACAnchorMemberListCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[ACAnchorMemberListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACAnchorMemberListCell"];
        }
        TRTCLiveUserInfo *userInfo = self.requestJoinAnchorArray[indexPath.row];
        cell.userInfo = userInfo;
        [cell.inviteAnchorBtn setTitle:@"允许上麦" forState:UIControlStateNormal];
        [cell.kickOutBtn setTitle:@"拒绝" forState:UIControlStateNormal];
        @weakify(self)
        cell.clickActionHandler = ^(NSInteger value) {
            @strongify(self)
            BOOL isAgree = value == 2 ? YES : NO;
            [self.liveRoom responseJoinAnchor:userInfo.userId agree: isAgree  reason:@""];
            if (isAgree) {
                [SVProgressHUD showInfoWithStatus:@"我同意了用户的发言请求"];
                if (self.removeUserHandler) {
                    self.removeUserHandler(userInfo,NO);
                }
            }else{
                [SVProgressHUD showInfoWithStatus:@"我拒绝了用户的发言请求"];
                if (self.removeUserHandler) {
                    self.removeUserHandler(userInfo,YES);
                }
            }
            [self.requestJoinAnchorArray removeObject:userInfo];
            [self.tableview reloadData];
            
        };
        return  cell;
    }
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

@end
